using System;
using System.Collections.Generic;

namespace snacks{
    class Snake {
        public bool IsDead { get; set; }
        public Vector HeadPos { get; set; }     
        public Vector HeadDir { get; set; }

        public Controller controller;
        Queue<Vector> body = new Queue<Vector>();
        Map map;
        int ownedLen;
        ConsoleColor color;
        GameManager gameManager;

        public Snake(GameManager gm, Map m,
        Vector pos, bool isConsole, int len, ConsoleColor col) {
            gameManager = gm;
            gameManager.Update += Move;
            HeadPos = pos;
            ownedLen = len;
            map = m;
            HeadPos = pos;
            HeadDir = new Vector(-1, 0);
            color = col;

            if(!isConsole)
                controller = new AIController(gameManager, map, this);
            else controller = new ConsoleController(this);

            body.Enqueue(pos);
            map[pos] = new BSnakeInfo(col);
        }

        void Move(){
            HeadDir = controller.GetDirection();
            Vector pos = HeadPos + HeadDir;
            if (map[pos].Type == BType.Snack) ownedLen += (map[pos] as BSnackInfo).Value;
            if (map[pos].Type == BType.Snack || map[pos].Type == BType.None)
            {
                HeadPos = pos;
                map[pos] = new BSnakeInfo(color);
                body.Enqueue(pos);
                if (ownedLen != 0) --ownedLen;
                else
                {
                    map[body.Dequeue()] = new BInfo(BType.None);
                }
            } else {
                //TODO: attribute
                Dead();
            }
        }

        void Dead()
        {
            IsDead = true;
            while (body.Count != 0)
            {
                map[body.Dequeue()] = new BSnackInfo(color, 1);
            }
            gameManager.Update -= Move;
        }
    }
}

/*
 * 1. Attribute
 */